<?php
namespace app\index\controller;

//使用时需要use一下
use think\Request;
use think\Db;
use think\Controller;

class Index extends Controller
{
	//测试数据库操作
	public function database()
	{
		//增加
		//Db::execute('insert into tp_data(name) values("dagang")');
		//修改
		//Db::execute('update tp_data set status=1 where id=3');
		//删除
		//Db::execute('delete from tp_data where id=4');
		//查询
		//dump(Db::query('select * from tp_data'));

		//链式操作
		//dump(Db::table('tp_data')->where('id=5')->select());
		//dump(Db::name('data')->where('id=5')->select());
		//dump(Db::name('data')->where('id>2')->find());
		//dump(Db::name('data')->where('id','>',2)->select());
		//dump(Db::name('data')->where('id','>=',0)->select());
		//dump(Db::name('data')->where('id','=',1)->select(false));
		/*
		dump(Db::name('data')
				->where([
							'id' => ['between','1,3'],
							'name'=> ['like','%xiao%']
						])
				->buildSql());
		dump(Db::name('data')->select(function($query){
			$query->where('id>3')->order('id desc');
		}));
		dump(Db::name('data')->where('id=1')->value('name'));
		dump(Db::name('data')
				->where('id>0')
				->column('id,name'));
		dump(Db::name('data')
				->whereTime('create_time','>','2017-1-7')
				->select());
			dump(Db::name('data')->whereTime('create_time','>','-2 days')->select());
		Db::name('data')->chunk(2,function($list){
			foreach ($list as $value) {
				dump($value['name']);
				if ($value['name'] == 'doudou') {
					return false;
				}
			}
			echo '=====';
		});*/
	Db::name('data')->chunk(2,function($list){
		foreach ($list as $value){
			dump($value['name']);
			if($value['name'] == 'jingjing'){
				return false;
			}
		}
		echo '============';
	});
	dump(Db::name('data')->field('id,name')->where('id','>',3)->fetchSql()->delete());
}
	//3、依赖注入
	public function qingqiu(Request $request)
	{
		//测试请求
		//1、传统方式，是单例对象
		//$request = Request::instance();
		//2、助手函数
		//$request = request();
		//获取URL
		//return $request->url(true);
		//return $request->domain();
		//return $request->ext();
		//return $request->module();
		//return $request->ip();
		//dump($request->param());
		//dump($request->only(['username']));
		//dump($request->except(['username']));
		//dump($request->has('username','get'));
		//dump(input('?get.id'));
		dump($request->get('username','abc','strtoupper'));
	}

	public function test($a,$b)
	{
		return $a + $b;
	}

	public function demo()
	{
		return __METHOD__;
	}

    public function index()
    {
	return $this->fetch();
    }
}
